<!DOCTYPE html>
<html lang="zh">
<head>
    <title>文件列表</title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=yes">
    <script type="text/javascript" src="jamjs/jquery-1.11.0.js"></script>
    <link rel="stylesheet" type="text/css" href="jamcss/JamTools.css">
    <style>
* {

                margin: 0;

                padding: 0;

            }

    </style>

</head>
<script type="text/javascript">
        $(document).ready(function(){
            $("#checkItems").click(function(){
                console.log("check")
                if($(this).prop("checked")){//被选中
                    $(":checkbox[name='checkboxitems']").prop("checked",true);
                }else{
                    $(":checkbox[name='checkboxitems']").prop("checked",false);
                }
            });
        });
  </script>
<body>
<div class="container">
    <input  type="button" class="back" id="backastep" style="background-color: #888888;" value="<返回上一级" >
    <input  type="button" class="back" id="backtohome" style="background-color: #888888;" value="⇱返回主目录" onClick="location='/home'">
<h2>当前位于{currentdir}<input  type="button" style="margin-right: 20px;float:right;background-color: darkred;"  value="退出登录" onClick="ExitLogin()"></h2>
    <input  type="button"  value="下载⇵" onClick="DownloadClick()">
    <input  type="button" value="新建文件夹" onClick="newdir()">
<!--canupload<input  type="button" value="上传文件" onClick="location='jamupload.html'">-->

</div>
<hr>
<table  id="filelist">
    <tr><td width="2%%" ><input type="checkbox"  name="checkItems" id="checkItems" /></td>
        <td width="40%%" class="filename">文件名</td>
        <td width="2%%"></td>
        <td width="15%%" class="fileprogress">类型/大小</td>
        <td width="25%%" class="filestatus">修改日期</td>
    </tr>
{dirlist}
<!--<tr>-->
<!--    <td width="2%%"><input type="checkbox"  name="checkboxitems" id="fa.png" /></td>-->
<!--    <td width="50%%"><a href="fa.png" target="_blank">机械制造基础-第五讲.pdf</a></td>-->
<!--    <td width="2%%"><a href="spark-md5.js" download=""><input type="image" src=jamhtmlpic/jamdowload.png style="border: 0;width:16px;height:16px;" /></a></td>-->
<!--    <td width="20%%">2.1MB</td><td width="20%%">2020-12-11 15:26:35</td>-->
<!--</tr>-->
<!--<tr>-->
<!--    <td width="2%%"><input type="checkbox"  name="checkboxitems" id="fga.png" /></td>-->
<!--    <td width="50%%"><a href="fa.png" target="_blank">222.pdf</a></td>-->
<!--    <td width="2%%"><input type="image" src=jamhtmlpic/jamdowload.png style="border: 0;width:16px;height:16px;" name="bt" id="245" /></td>-->
<!--    <td width="20%%">2.1MB</td><td width="20%%">2020-12-11 15:26:35</td>-->
<!--</tr>-->

</table>
<hr>
<div id="footer" >
        <p>Posted By: <a href="https://github.com/fandesfyf/JamTools">JamTools</a> | Contact Information: <a href="mailto:fandesfyf@qq.com">fandesfyf@qq.com</a>.</p></div>
<script type="text/javascript">
    let backastepbtn = $("#backastep");
    if (window.location.pathname==="/home"){
        backastepbtn.remove();
        $("#backtohome").remove();
    }
    String.prototype.trim = function (char, type) {
        if (char) {
        if (type === 'left') {
        return this.replace(new RegExp('^\\'+char+'+', 'g'), '');

        } else if (type === 'right') {
        return this.replace(new RegExp('\\'+char+'+$', 'g'), '');

        }

        return this.replace(new RegExp('^\\'+char+'+|\\'+char+'+$', 'g'), '');

        }

        return this.replace(/^\s+|\s+$/g, '');

    };
    backastepbtn.click(function (){
        String.prototype.replaceAll = function(s1, s2) {
            return this.replace(new RegExp(s1, "gm"), s2);
        }

        var fullPath = window.location.pathname.replaceAll('/', '\\').trim('\\', 'right');
        var pos = fullPath.lastIndexOf('\\');
        var newloaction = fullPath.substr(0, pos);
        console.log(newloaction)
        if (newloaction===""){
            newloaction="/home";
        }
        window.location=newloaction;

    });
    function newdir(){
        var dirname = prompt("请输入新文件夹名称：", "新建文件夹");
        if (dirname !== null && dirname !== "") {
            console.log("新建文件夹:"+dirname)
            var fdjson={};
            fdjson.requesttype="createdir";
            fdjson.dirname= dirname;
            fdjson.location=window.location.pathname.replace("/jamupload.html","");
            console.log("stop:",fdjson)

            var xhr = new XMLHttpRequest();

            xhr.open('post', '/createdir', true);

            xhr.onreadystatechange = function (){
                if (xhr.readyState === 4 ){
                    if (xhr.status === 200) {
                    console.log("已创建");
                    window.location.reload();
                    }else {
                        alert("创建文件夹失败");
                    }
                }
            };
            xhr.send(JSON.stringify(fdjson));
        }
    }
    var downlist=[];
    function DownloadClick() {
        console.log("downlist click");
        downlist.length=0;
        $(":checkbox[name='checkboxitems']").each(function(){
        if($(this).prop("checked")){//如果被选中
            var url=$(this).attr("id");
            downlist.push(url);
            console.log(url);
        }
        });
        if (downlist.length===0){
            alert("请先选中要下载的文件!")
        }else {allDownload();}


    }
    function ExitLogin(){
        console.log("exit")
        document.cookie= "p9az=; expires=Thu, 01 Jan 1970 00:00:00 GMT";
        document.cookie= "p2iix9ab=; expires=Thu, 01 Jan 1970 00:00:00 GMT";//清除cookie
        open(location, '_self').close()
    }

    function allDownload() {
        for (let index = 0; index < downlist.length; index++) {
            downloadFile(downlist[index]);
        }
    }
    function downloadFile(url){
          // var a = document.createElement("a");// 创建a标签
          //   a.href = url;                  // 设置下载地址
          //   a.download="";
          //   // a.download = name;              // 设置下载文件名
          //   document.body.append(a);        // 把标签添加到文档中
          //   a.click();                      // 执行click(模拟点击下载)事件
          //   a.remove();                     // 删除a标签
        let ele = document.createElement('a')
        ele.download = ""
        ele.style.display = 'none'
        ele.href = url
        document.body.appendChild(ele)
        ele.click()
        document.body.removeChild(ele)

    }
    document.onchange=function (){
          console.log("change")
          footerchange();
      }
    function footerchange(){
       var _wh = $(window).height();
      var _bh = $(document.body).height();
      if (_bh+50 < _wh) {
          console.log("1111")
        $("#footer").css({
          position: "fixed",
          bottom: "0",
          left: "0",
        })
      } else {
          console.log('2222')
        $("#footer").css({
          position: "static",
          bottom: "auto",
          left: "auto",
        })
      }
    }
    footerchange();
</script>
</body>

</html>
